﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using XYTXL.AppCode;
using XYTXL.Data;
using XYTXL.API;

namespace XYTXL.View
{
    public partial class RoleList : BasePage
    {
        public string strSearchZM = "<span style='color:#ff7e00'>ALL</span><span>系统角色</span><span>不可用角色</span>";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            DataTable dt = new DataTable();
            dt = new JH_Auth_RoleB().GetEntities(d => d.PRoleCode != -1).OrderByDescending(d => d.isSysRole).ToDataTable();

            List<string> ListSerZM = new List<string>();
            //将中文转为汉字
            dt.Columns.Add("SerZM");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string strZM = ConvertLetters(dt.Rows[i]["RoleName"].ToString()).Substring(0, 1);
                dt.Rows[i]["SerZM"] = strZM;
                if (!ListSerZM.Contains(strZM))
                {
                    ListSerZM.Add(strZM);
                }
            }
            RptRole.DataSource = dt;
            RptRole.DataBind();

            foreach (string item in ListSerZM)
            {
                strSearchZM = strSearchZM + " <span>" + item + "</span>";
            }
        }

        public String ConvertLetters(String chinese)
        {
            try
            {
                char[] buffer = new char[chinese.Length];
                for (int i = 0; i < chinese.Length; i++)
                {
                    buffer[i] = ConvertChar(chinese[i]);
                }
                return new String(buffer).Replace(" ", "");
            }
            catch
            {
                return "";
            }
        }

        public char ConvertChar(Char chinese)
        {
            try
            {
                Encoding gb2312 = Encoding.GetEncoding("GB2312");
                Encoding unicode = Encoding.Unicode;
                byte[] unicodeBytes = unicode.GetBytes(new Char[] { chinese });
                byte[] asciiBytes = Encoding.Convert(unicode, gb2312, unicodeBytes);
                if (asciiBytes.Length < 2)
                    return chinese; //待转换的字符在ascii码表示范围内
                int n = (int)asciiBytes[0] << 8;
                n += (int)asciiBytes[1];
                //   根据汉字区域码获取拼音声母
                if (In(0xB0A1, 0xB0C4, n)) return 'A';
                if (In(0XB0C5, 0XB2C0, n)) return 'B';
                if (In(0xB2C1, 0xB4ED, n)) return 'C';
                if (In(0xB4EE, 0xB6E9, n)) return 'D';
                if (In(0xB6EA, 0xB7A1, n)) return 'E';
                if (In(0xB7A2, 0xB8c0, n)) return 'F';
                if (In(0xB8C1, 0xB9FD, n)) return 'G';
                if (In(0xB9FE, 0xBBF6, n)) return 'H';
                if (In(0xBBF7, 0xBFA5, n)) return 'J';
                if (In(0xBFA6, 0xC0AB, n)) return 'K';
                if (In(0xC0AC, 0xC2E7, n)) return 'L';
                if (In(0xC2E8, 0xC4C2, n)) return 'M';
                if (In(0xC4C3, 0xC5B5, n)) return 'N';
                if (In(0xC5B6, 0xC5BD, n)) return 'O';
                if (In(0xC5BE, 0xC6D9, n)) return 'P';
                if (In(0xC6DA, 0xC8BA, n)) return 'Q';
                if (In(0xC8BB, 0xC8F5, n)) return 'R';
                if (In(0xC8F6, 0xCBF0, n)) return 'S';
                if (In(0xCBFA, 0xCDD9, n)) return 'T';
                if (In(0xCDDA, 0xCEF3, n)) return 'W';
                if (In(0xCEF4, 0xD188, n)) return 'X';
                if (In(0xD1B9, 0xD4D0, n)) return 'Y';
                if (In(0xD4D1, 0xD7F9, n)) return 'Z';
                return ' ';
            }
            catch
            {
                return ' ';
            }
        }

        private bool In(int Lp, int Hp, int Value)
        {
            return ((Value <= Hp) && (Value >= Lp));
        }
    }
}